package com.example.gank.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.android.core.base.BaseActivity;
import com.example.gank.R;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.pb_progress)
    ProgressBar pbProgress;
    @BindView(R.id.wv_detial)
    WebView wvDetial;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void onInitView(Bundle bundle) {
        wvDetial.loadUrl(getIntent().getStringExtra("url"));
        wvDetial.getSettings().setJavaScriptEnabled(true);
        wvDetial.setWebChromeClient(new WebChromeClient() {//监听网页加载
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    pbProgress.setVisibility(View.GONE);
                } else {
                    // 加载中
                    pbProgress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        wvDetial.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                //用javascript隐藏系统定义的404页面信息
                String data = "Page NO FOUN！";
                view.loadUrl("javascript:document.body.innerHTML=\"" + data + "\"");
            }
        });
    }
}
